// Copyright © 2022 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package vulnerability

import (
	"testing"
)

func Test_GetCVSSV3VersionFromVector(t *testing.T) {
	type args struct {
		cvssV3Vector string
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		{
			name: "sanity 1",
			args: args{
				cvssV3Vector: "CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:L/A:H",
			},
			want: "3.0",
		},
		{
			name: "sanity 2",
			args: args{
				cvssV3Vector: "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:L/A:H",
			},
			want: "3.1",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := GetCVSSV3VersionFromVector(tt.args.cvssV3Vector); got != tt.want {
				t.Errorf("GetCVSSV3VersionFromVector() = %v, want %v", got, tt.want)
			}
		})
	}
}

func Test_ExploitScoreAndImpactScoreFromV3Vector(t *testing.T) {
	type args struct {
		cvssV3Vector string
	}
	tests := []struct {
		name  string
		args  args
		want1 float64
		want2 float64
	}{
		{
			name: "sanity 1",
			args: args{
				cvssV3Vector: "CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N",
			},
			want1: 3.9,
			want2: 3.6,
		},
		{
			name: "sanity 2",
			args: args{
				cvssV3Vector: "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N",
			},
			want1: 3.9,
			want2: 1.4,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got1, got2 := ExploitScoreAndImpactScoreFromV3Vector(tt.args.cvssV3Vector); got1 != tt.want1 || got2 != tt.want2 {
				t.Errorf("ExploitScoreAndImpactScoreFromV3Vector() = %v, %v want %v, %v", got1, got2, tt.want1, tt.want2)
			}
		})
	}
}

func Test_ExploitScoreAndImpactScoreFromV2Vector(t *testing.T) {
	type args struct {
		cvssV2Vector string
	}
	tests := []struct {
		name  string
		args  args
		want1 float64
		want2 float64
	}{
		{
			name: "sanity 1",
			args: args{
				cvssV2Vector: "AV:N/AC:L/Au:N/C:P/I:N/A:N",
			},
			want1: 10,
			want2: 2.9,
		},
		{
			name: "sanity 2",
			args: args{
				cvssV2Vector: "AV:L/AC:M/Au:N/C:P/I:N/A:N",
			},
			want1: 3.4,
			want2: 2.9,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got1, got2 := ExploitScoreAndImpactScoreFromV2Vector(tt.args.cvssV2Vector); got1 != tt.want1 || got2 != tt.want2 {
				t.Errorf("ExploitScoreAndImpactScoreFromV2Vector() = %v, %v want %v, %v", got1, got2, tt.want1, tt.want2)
			}
		})
	}
}
